
var canvas, context, stats;
var time = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

var controls;
var scene;
var planes = [];
var bullets = [];
var missiles = [];
var player;

requestAnimFrame = (function() {
  return window.requestAnimationFrame ||
     window.webkitRequestAnimationFrame ||
     window.mozRequestAnimationFrame ||
     window.oRequestAnimationFrame ||
     window.msRequestAnimationFrame ||
     function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
       window.setTimeout(callback, 1000/60);
     };
})();

init();
animate();

function init() {
  canvas = document.createElement('canvas');
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  document.body.appendChild(canvas);
  context = canvas.getContext("2d");

  stats = new Stats();
  stats.domElement.style.position = 'absolute';
  stats.domElement.style.top = '0px';
  stats.domElement.style.zIndex = 100;
  document.body.appendChild(stats.domElement);

  window.addEventListener('resize', onWindowResize, false);


  var image = new Image();
  image.src = 'f16.png';
  var shadowImage = new Image();
  shadowImage.src = 'plane.png';  
  player = new Plane(image, shadowImage, 100, -200, bullets, missiles);
  //player.speed = 600;
  planes.push(player);
  planes.push(new Plane(image, image, 200, -150, bullets, missiles));

  scene = new Scene(context, player);
  controls = new Controls(player);
  time = Date.now();
}

function onWindowResize() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}


function animate() {
  requestAnimFrame(animate);

  var newTime = Date.now();
  var dt = (newTime - time) / 1000;
  time = newTime;

  update(dt);
  render();
  stats.update();
}

function update(dt) {
  scene.update(dt);
  controls.update(dt);
  for (var i = 0; i < missiles.length; i++) {
    if (!missiles[i].update(dt, planes)) { 
      missiles.splice(i, 1);
      i--;
    }
    else if (missiles[i].y > 0) { 
      var life = missiles[i].speed/10;
      scene.addWaves(missiles[i].x, life);
      missiles.splice(i, 1);
      i--;
    }
  }
  for (var i = 0; i < bullets.length; i++) {
    if (!bullets[i].update(dt)) {
      bullets.splice(i, 1);
      i--;
    }
    else if (bullets[i].y > 0) { 
      var life = 30;
      scene.addWaves(bullets[i].x, life);
      bullets.splice(i, 1);
      i--;
    }
  }
  for (var i = 0; i < planes.length; i++) {
      planes[i].update(dt);
      if (planes[i].y > -200 && Math.random() < 0.5) {
        var life = (planes[i].y + 200) * 4;
        scene.addWaves(planes[i].x - 75 * Math.cos(planes[i].angle),
          life);
      }
      if (planes[i].y > 0) {
        planes[i].y = 0;
        planes[i].angle = -planes[i].angle;
      }
  }
}

function render() {
  //context.clearRect(0, 0, canvas.width, canvas.height);
  context.fillStyle = 'rgb(175, 210, 255)';
  context.fillRect(0, 0, canvas.width, canvas.height);

  context.save();
  
  context.translate(-player.x + canvas.width/2, -player.y + canvas.height/2);
  scene.renderBackground(context);
  for (var i = 0; i < planes.length; i++) {
      planes[i].render(context);
  }
  for (var i = 0; i < bullets.length; i++) {
    bullets[i].render(context);
  }
  for (var i = 0; i < missiles.length; i++) {
    missiles[i].render(context);
  }
  scene.renderForeground(context);

  context.restore();
}



function generateShadowImage(image) {
  var w = image.width;
  var h = image.height;

  var canvas = document.createElement("canvas");
  canvas.width = w;
  canvas.height = h;

  var context = canvas.getContext("2d");
  context.drawImage(image, 0, 0);

  var pixels = context.getImageData(0, 0, w, h).data;

  for (var i = 0; i < pixels.length; i += 4) {
      pixels[i  ] = 0;
      pixels[i+1] = 0;
      pixels[i+2] = 0;
      pixels[i+3] = pixels[i+3];
  }

  context.putImageData(pixels, 0, 0);
  var shadowImage = new Image();
  shadowImage.src = canvas.toDataURL();
  return shadowImage;
}
